﻿// =================================================================== 
// 项目说明
//====================================================================
// liucx @Copy Right 2019
// 文件： CouponRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/11/29
// 负责人：liucx
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    /// 仓储层
    /// </summary>	
    public class CouponRepository : BaseRepository<Coupon>
    {

        #region 构造

        public CouponRepository() : base() { }

        public CouponRepository(string connectionString) : base(connectionString) { }

        public CouponRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion
        public Page<CouponDto> Pages(int pageIndex, int pageSize, CouponSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT T1.*,T2.CardUserId,T2.ExpirationDate,T2.UsedStoreID,T2.UsedDate,T2.UseStatus,T3.UserName,T3.Mobile as UserMobile,T4.WashName as UsedStoreName FROM [Coupon] T1 WITH(NOLOCK) ");
            sb.Append(" Left join CouponUserRela T2 on T1.CouponID= T2.CouponID");
            sb.Append(" Left join UserInfo T3 on T2.CardUserId= T3.UserID");
            sb.Append(" Left join WashShop T4 on T2.UsedStoreID= T4.WashID");
            sb.Append(" WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }
            sb.Append(" ORDER BY T1.CouponID DESC");
            return dbContext.Connection.Page<CouponDto>(pageIndex, pageSize, sb.ToString(), dparams);
        }

        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(CouponSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {

            if (!string.IsNullOrWhiteSpace(model.CouponCode))
            {
                sb.Append(" AND charindex(@CouponCode,T1.CouponCode)>0");
                dparams.Add("CouponCode", model.CouponCode, System.Data.DbType.String);
            }
            if (model.Batch > 0)
            {
                sb.Append(" AND T1.Batch=@Batch");
                dparams.Add("Batch", model.Batch, System.Data.DbType.Int32);
            }
            if (model.CouponStatus >= 0)
            {
                sb.Append(" AND T1.CouponStatus=@CouponStatus");
                dparams.Add("CouponStatus", model.Batch, System.Data.DbType.Int32);
            }
            if (model.CouponType >= 0)
            {
                sb.Append(" AND T1.CouponType=@CouponType");
                dparams.Add("CouponType", model.CouponType, System.Data.DbType.Int32);
            }
        }

        /// <summary>
        /// 获取未使用
        /// </summary>
        /// <returns></returns>
        public Coupon GetNoUse()
        {
            string sql = "SELECT * FROM [Coupon] T1 WITH(NOLOCK) WHERE T1.IsActive=1 AND CouponStatus=0";
            return FirstOrDefault(sql);
        }
    }
}